home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / sort / Source / sortMenus.c < prev    next >
Text File  |  1993-06-17  |  12KB  |  477 lines

  1. /*****
  2.  * sortMenus.c
  3.  *
  4.  *
  5.  *****/
  6. #include <Menus.h>
  7. #include <Windows.h>
  8. #include <ToolUtils.h>
  9. #include <Desk.h>
  10. #include <Resources.h>
  11. #include <Memory.h>
  12. #include <Packages.h>
  13. #include <Dialogs.h>
  14.  
  15. #include "sortWindow.h"
  16. #include "sortMenus.h"
  17. #include "sortdata.h"
  18. #include "sort.h"
  19. #include "acur.h"
  20. #include "notif.h"
  21.  
  22. #define CONFIG_DATAID    128
  23. #define CONFIG_DISPID    129
  24. #define DONEALERTID        129
  25. #define CANCELALERTID    130
  26. #define ABOUTALERTID    131
  27.  
  28. extern    WindowPtr sortWindow;
  29. extern Boolean done;
  30.  
  31. #define NUMDATAITEM    4
  32. #define MINDATAITEM    6
  33. #define MAXDATAITEM 8
  34. #define OK_USERITEM 9
  35.  
  36.  
  37. #define HILITECOMPITEM 3
  38. #define HILITESWAPITEM 4
  39. #define HILITEDELITEM 5
  40. #define UNHILITEDELITEM 6
  41.  
  42.  
  43. MenuHandle    appleMenu, fileMenu, editMenu, sortMenu, configureMenu, DebugMenu;
  44. short stop_sort;
  45. Str255 sort_name;
  46.  
  47.  
  48. enum    {
  49.     appleID = 1,
  50.     fileID,
  51.     editID,
  52.     sortID,
  53.     configureID,
  54.     DebuggerID
  55.     };
  56.  
  57. enum    {
  58.     openItem = 1,
  59.     closeItem,
  60.     quitItem = 4
  61.     };
  62.  
  63. enum {
  64.     about_Item = 1
  65. };
  66.  
  67. enum {
  68.     configdata_Item = 1,
  69.     configdisp_Item = 2
  70. };
  71.  
  72. enum    {
  73.     stop_sortItem = 1
  74.     };
  75.  
  76. enum {
  77.     gointoDebugger = 1
  78.     };
  79.  
  80. /****
  81.  * SetUpMenus()
  82.  *
  83.  *    Set up the menus. Normally, we’d use a resource file, but
  84.  *    for this example we’ll supply “hardwired” strings.
  85.  *
  86.  ****/
  87.  
  88. void SetUpMenus(void)
  89.  
  90. {
  91.     stop_sort = false;
  92.  
  93.     InsertMenu(appleMenu = NewMenu(appleID, "\p\024"), 0);
  94.     InsertMenu(fileMenu = NewMenu(fileID, "\pFile"), 0);
  95.     InsertMenu(editMenu = NewMenu(editID, "\pEdit"), 0);
  96.     InsertMenu(sortMenu = NewMenu(sortID, "\pSort"), 0);
  97.     InsertMenu(configureMenu = NewMenu(configureID, "\pConfigure"), 0);
  98.     InsertMenu(DebugMenu = NewMenu(DebuggerID, "\pDebug"), 0);
  99.     
  100.     DrawMenuBar();
  101.     AppendMenu(appleMenu, "\pAbout Sort Demo…;(-");
  102.     AddResMenu(appleMenu, 'DRVR');
  103.     AppendMenu(fileMenu, "\pOpen/O;Close/W;(-;Quit/Q");
  104.     AppendMenu(editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  105.     AppendMenu(sortMenu, "\pStop/.;(-");
  106.     AddResMenu(sortMenu, 'SORT');
  107.     AppendMenu(configureMenu, "\pConfigure Data…;Configure Display…");
  108.     AppendMenu(DebugMenu, "\pGoInto Debugger/D");
  109. }
  110. /* end SetUpMenus */
  111.  
  112.  
  113. /****
  114.  *  AdjustMenus()
  115.  *
  116.  *    Enable or disable the items in the Edit menu if a DA window
  117.  *    comes up or goes away. Our application doesn't do anything with
  118.  *    the Edit menu.
  119.  *
  120.  ****/
  121.  
  122. int enable (MenuHandle menu, short item, short ok);
  123.  
  124. void AdjustMenus(void)
  125. {
  126.     register WindowPeek wp = (WindowPeek) FrontWindow();
  127.     short kind = wp ? wp->windowKind : 0;
  128.     short num, count;
  129.     
  130.     Boolean DA = kind < 0;
  131.     Boolean invalid;
  132.     
  133.     enable(editMenu, 1, DA);
  134.     enable(editMenu, 3, DA);
  135.     enable(editMenu, 4, DA);
  136.     enable(editMenu, 5, DA);
  137.     enable(editMenu, 6, DA);
  138.     
  139.     invalid = (((WindowPeek) sortWindow)->visible == true) ||
  140.               (mindataval < 1) || 
  141.               (maxdataval > (long)windowBounds.right - 10) ||
  142.               (height < 1L);
  143.  
  144.     enable(fileMenu, openItem, invalid == false);
  145.     enable(fileMenu, closeItem, DA || ((WindowPeek) sortWindow)->visible);
  146.  
  147.     num = CountMItems(sortMenu);
  148.     enable(sortMenu, stop_sortItem, sorting);
  149.     for (count = stop_sortItem + 1; count <= num; count++) {
  150.         enable(sortMenu, count, !sorting);
  151.     }
  152.     enable(configureMenu, configdata_Item, !sorting);
  153.     enable(configureMenu, configdisp_Item, true);
  154.     enable(DebugMenu, configdisp_Item, true);
  155.  
  156. }
  157.  
  158.  
  159. static
  160. enable(MenuHandle menu, short item, short ok)
  161. {
  162.     if (ok)
  163.         EnableItem(menu, item);
  164.     else
  165.         DisableItem(menu, item);
  166. }
  167.  
  168.  
  169. /*****
  170.  * HandleMenu(mSelect)
  171.  *
  172.  *    Handle the menu selection. mSelect is what MenuSelect() and
  173.  *    MenuKey() return: the high word is the menu ID, the low word
  174.  *    is the menu item
  175.  *
  176.  *****/
  177.  
  178. void HandleMenu (long mSelect)
  179.  
  180. {
  181.     int            menuID = HiWord(mSelect);
  182.     short            menuItem = LoWord(mSelect);
  183.     short            ihit;
  184.     Str255        name,excs,cmps, ticks;
  185.     GrafPtr        savePort;
  186.     WindowPeek    frontWindow;
  187.     DialogPtr    dialogwindow;
  188.     Handle h;
  189.     sortptr srt;
  190.     long starrttime, finishtime;
  191.  
  192.     switch (menuID)
  193.       {
  194.       case    appleID:
  195.           if (menuItem != about_Item) {
  196.             GetPort(&savePort);
  197.             GetItem(appleMenu, menuItem, name);
  198.             OpenDeskAcc(name);
  199.             SetPort(savePort);
  200.         }
  201.         else {
  202.             Alert(ABOUTALERTID, NULL);
  203.         }
  204.         break;
  205.     
  206.       case sortID:
  207.           sorting = true;
  208.           switch (menuItem)
  209.             {
  210.  
  211.             case stop_sortItem:
  212.                 stop_sort = true;
  213.             return;
  214.  
  215.                 default:
  216.                 HiliteMenu(0);
  217.                 GetItem(sortMenu, menuItem, sort_name);
  218.                     numexchanges = 0;
  219.                     numcompares = 0;
  220.                     if (init_data(numdataitems) != noErr) {
  221.                         numdataitems = 2;
  222.                     height=((((windowBounds.bottom - windowBounds.top) - numdataitems) -5)) / (numdataitems + 1);
  223.                         init_data(numdataitems);
  224.                     }
  225.                     h = GetNamedResource('SORT', sort_name);
  226.                     LoadResource(h);
  227.                     HLock(h);
  228.                     HNoPurge(h);
  229.                 starrttime = TickCount();
  230.                 srt = (sortptr)(*h);
  231.                 (*srt)(numdataitems, sortdata, swap, compare, &stop_sort);
  232.                 finishtime = TickCount();
  233.                 ReleaseResource(h);
  234.                 break;
  235.                 
  236.  
  237.             }
  238.           SetCursor(&qd.arrow);
  239.             if (gInBackground) {
  240.                 wait_Foreground(1);
  241.             }
  242.           NumToString(finishtime - starrttime, (StringPtr)&ticks);
  243.             NumToString((long)numdataitems, (StringPtr) &name);
  244.             NumToString(numcompares, (StringPtr) &cmps);
  245.             NumToString(numexchanges, (StringPtr) &excs);
  246.           
  247.             ParamText((StringPtr)&name, (StringPtr)&cmps, (StringPtr)excs, (StringPtr)&ticks);
  248.             if (!stop_sort) {
  249.                   data_sorted = true;
  250.                 Alert(DONEALERTID, NULL);
  251.             }
  252.             else {
  253.                 Alert(CANCELALERTID, NULL);
  254.             }
  255.           stop_sort = false;
  256.             sorting = false;
  257.             break;
  258.  
  259.       case    fileID:
  260.         switch (menuItem)
  261.           {
  262.             case openItem:
  263.                 ShowWindow(sortWindow);
  264.                 SelectWindow(sortWindow);
  265.             break;
  266.                                 
  267.             case closeItem:
  268.                 if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  269.                   break;
  270.                   
  271.                 if (frontWindow->windowKind < 0)
  272.                     CloseDeskAcc(frontWindow->windowKind);
  273.                 else if (frontWindow = (WindowPeek) sortWindow)
  274.                     HideWindow(sortWindow);
  275.             break;
  276.   
  277.             case    quitItem:
  278.                   DisposAcur();
  279.                 done = true;
  280.             break;
  281.             
  282.           }
  283.         break;
  284.                   
  285.       case    configureID:
  286.           {
  287.               Str255 numString;
  288.               Rect box;
  289.               Handle hItem;
  290.               short itemType;
  291.               long newnumdata, newmindata, newmaxdata, newheight,
  292.                   newhilitedelay, newunhilitedelay;
  293.               Boolean  invalid;
  294.  
  295.             switch (menuItem) {
  296.                 case configdata_Item:
  297.      
  298.                     dialogwindow = GetNewDialog(CONFIG_DATAID, nil, (WindowPtr) -1);
  299.  
  300.  
  301. //    Fix ME                GetDItem (dialogwindow, OK_USERITEM, &itemType, &hItem, &box);
  302. //                    SetDItem (dialogwindow, OK_USERITEM, itemType, (Handle)FrameUserProc , &box);
  303.         
  304.                     GetDItem (dialogwindow, NUMDATAITEM, &itemType, &hItem, &box);
  305.                     NumToString (numdataitems, (StringPtr)&numString);
  306.                     SetIText (hItem, (StringPtr)&numString);
  307.         
  308.         
  309.                     GetDItem (dialogwindow, MINDATAITEM, &itemType, &hItem, &box);
  310.                     NumToString (mindataval, (StringPtr)&numString);
  311.                     SetIText (hItem, (StringPtr)&numString);
  312.         
  313.         
  314.                     GetDItem (dialogwindow, MAXDATAITEM, &itemType, &hItem, &box);
  315.                     NumToString (maxdataval, (StringPtr)&numString);
  316.                     SetIText (hItem, (StringPtr)&numString);
  317.         
  318.         
  319.                     do {
  320.                         invalid = false;
  321.                         do {
  322.                             ModalDialog (nil, &ihit);
  323.                         } while ((ihit != ok) && (ihit != cancel));
  324.         
  325.                         if (ihit == ok) {
  326.                             GetDItem (dialogwindow, NUMDATAITEM, &itemType, &hItem, &box);
  327.                             GetIText (hItem, (StringPtr)&numString);
  328.                             StringToNum ((StringPtr)&numString, &newnumdata);
  329.                             if (newnumdata < 2L) {
  330.                                 SysBeep(1);
  331.                                 invalid = true;
  332.                                 continue;
  333.                             }
  334.         
  335.         
  336.                             GetDItem (dialogwindow, MINDATAITEM, &itemType, &hItem, &box);
  337.                             GetIText (hItem, (StringPtr)&numString);
  338.                             StringToNum ((StringPtr)&numString, &newmindata);
  339.                             if (((WindowPeek) sortWindow)->visible)    {
  340.                                 if ((newmindata < 1) || (newmindata > (long)windowBounds.right - 10)) {
  341.                                     SysBeep(1);
  342.                                     invalid = true;
  343.                                     continue;
  344.                                 }
  345.                             }
  346.         
  347.         
  348.                             GetDItem (dialogwindow, MAXDATAITEM, &itemType, &hItem, &box);
  349.                             GetIText (hItem, (StringPtr)&numString);
  350.                             StringToNum ((StringPtr)&numString, &newmaxdata);
  351.                             if (((WindowPeek) sortWindow)->visible)    {
  352.                                 if ((newmaxdata < newmindata) || (newmaxdata > (long)windowBounds.right - 10)) {
  353.                                     SysBeep(1);
  354.                                     invalid = true;
  355.                                     continue;
  356.                                 }
  357.                             }
  358.                             
  359.                             newheight=((((windowBounds.bottom - windowBounds.top) - newnumdata) -5)) / (newnumdata + 1);
  360.                             if (((WindowPeek) sortWindow)->visible)    {
  361.                                 if ((newheight < 1L)) {
  362.                                     SysBeep(1);
  363.                                     invalid = true;
  364.                                     continue;
  365.                                 }
  366.                             }
  367.                             else {
  368.                                 if (newmaxdata < newmindata) {
  369.                                     SysBeep(1);
  370.                                     invalid = true;
  371.                                     continue;
  372.                                 }
  373.                                 invalid = (newmindata < (long)1) || 
  374.                                           (newmaxdata > (long)windowBounds.right - 10) ||
  375.                                           (newheight < 1L);
  376.         
  377.                                 enable(fileMenu, openItem, invalid == false);
  378.                     
  379.                                 invalid = false;
  380.                             }
  381.                             SetPort(sortWindow);
  382.                             EraseRect(&sortWindow->portRect);
  383.         
  384.                             numdataitems = newnumdata;
  385.                             mindataval = newmindata;
  386.                             maxdataval = newmaxdata;
  387.         
  388.                             height=((((windowBounds.bottom - windowBounds.top) - numdataitems) -5)) / (numdataitems + 1);
  389.                                 data_sorted = true;
  390.                                 if (init_data(numdataitems) != noErr) {
  391.                                     numdataitems = 2;
  392.                                 height=((((windowBounds.bottom - windowBounds.top) - numdataitems) -5)) / (numdataitems + 1);
  393.                                     init_data(numdataitems);
  394.                                 }
  395.                         }
  396.                     } while (invalid);
  397.                     
  398.                     DisposeDialog(dialogwindow);
  399.                 break;
  400.             
  401.             case configdisp_Item:
  402.                     dialogwindow = GetNewDialog(CONFIG_DISPID, nil, (WindowPtr) -1);
  403.         
  404. //    FIX ME                GetDItem (dialogwindow, OK_USERITEM, &itemType, &hItem, &box);
  405. //                    SetDItem (dialogwindow, OK_USERITEM, itemType, (Handle)FrameUserProc , &box);
  406.  
  407.                     GetDItem (dialogwindow, HILITEDELITEM, &itemType, &hItem, &box);
  408.                     NumToString (hilitedelay, (StringPtr)&numString);
  409.                     SetIText (hItem, (StringPtr)&numString);
  410.         
  411.                     GetDItem (dialogwindow, UNHILITEDELITEM, &itemType, &hItem, &box);
  412.                     NumToString (unhilitedelay, (StringPtr)&numString);
  413.                     SetIText (hItem, (StringPtr)&numString);
  414.  
  415.  
  416.                     GetDItem (dialogwindow, HILITESWAPITEM, &itemType, &hItem, &box);
  417.                     SetCtlValue((ControlHandle)hItem, (short)hilite_exchange);
  418.         
  419.                     GetDItem (dialogwindow, HILITECOMPITEM, &itemType, &hItem, &box);
  420.                     SetCtlValue((ControlHandle)hItem, (short)hilite_compare);
  421.  
  422.                     do {
  423.                         invalid = false;
  424.                         do {
  425.                             ModalDialog (nil, &ihit);
  426.                             if ((ihit == HILITESWAPITEM) || (ihit == HILITECOMPITEM)) {
  427.                                 GetDItem (dialogwindow, ihit, &itemType, &hItem, &box);
  428.                                 SetCtlValue((ControlHandle)hItem, 1  - GetCtlValue((ControlHandle)hItem));
  429.                             }
  430.                         } while ((ihit != ok) && (ihit != cancel));
  431.                         if (ihit == ok) {
  432.                             GetDItem (dialogwindow, HILITEDELITEM, &itemType, &hItem, &box);
  433.                             GetIText (hItem, (StringPtr)&numString);
  434.                             StringToNum ((StringPtr)&numString, &newhilitedelay);
  435.                             GetDItem (dialogwindow, UNHILITEDELITEM, &itemType, &hItem, &box);
  436.                             GetIText (hItem, (StringPtr)&numString);
  437.                             StringToNum ((StringPtr)&numString, &newunhilitedelay);
  438.                             if ((newhilitedelay < 0L) || (newunhilitedelay < 0L)) {
  439.                                 SysBeep(1);
  440.                                 invalid = true;
  441.                                 continue;
  442.                             }
  443.                             hilitedelay = newhilitedelay;
  444.                             unhilitedelay = newunhilitedelay;
  445.     
  446.                             GetDItem (dialogwindow, HILITESWAPITEM, &itemType, &hItem, &box);
  447.                             hilite_exchange = (Boolean) GetCtlValue((ControlHandle)hItem);
  448.  
  449.                             GetDItem (dialogwindow, HILITECOMPITEM, &itemType, &hItem, &box);
  450.                             hilite_compare = (Boolean) GetCtlValue((ControlHandle)hItem);
  451.  
  452.                             DrawSort(true);
  453.                         }
  454.                     } while(invalid);
  455.                     DisposeDialog(dialogwindow);
  456.                 break;
  457.                 
  458.             }
  459.             
  460.         }
  461.         break;
  462.  
  463.       case    editID:
  464.         if (!SystemEdit(menuItem-1))
  465.           SysBeep(5);
  466.         break;
  467.         
  468.         case DebuggerID:
  469.             Debugger();
  470.         break;
  471.       }
  472.       
  473.       HiliteMenu(0);
  474. }
  475. /* end HandleMenu */
  476.  
  477.